﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using evt;
public class ZjhSettlement : MonoBehaviour {

	// Use this for initialization
	private List<SettlementCell> list = new List<SettlementCell>();
	public Zjh.ResRoundOverMsg msg = null;
	public List<ZjhPlayer> playerList = null;
	public static bool GameAllOver = false;
	public static bool Settlement = false;
	void Start () {
		EventTriggerListener.Get (transform.GetChild (2).gameObject).onClick = Close;
	
	}

	private void Close(GameObject go){
		ZjhSettlement.Settlement = false;
		transform.localPosition = new Vector3(10000,0,0);
		ServerEventDispather.getInstance ().Add(ServerEvent.RESET_ZJH_CARD,null);
		if ( GameAllOver ) {
			GameAllOver = false;
			GameObject.Find ("SumUpCalculation").GetComponent<ZJHSumUpCalculation> ().playerList = playerList;
			GameObject.Find ("SumUpCalculation").GetComponent<ZJHSumUpCalculation> ().Show ();
		}
	}

	public void ShowView(float time){
		InvokeRepeating ("StartShow",time,0f);
	}

	private void StartShow(){
		init ();
		ServerEventDispather.getInstance ().AddEventDispather (ServerEvent.HIDE_SETUP_VIEW, null);
		SoundPlayer.play ("结算");
		transform.localPosition = Vector3.zero;
	}

	private void init(){
		ZjhView.Igame = false;
		if ( list.Count > 0) {
			for (int i = 0; i < list.Count; i++) {
				Destroy (list [i].gameObject);
			}
		}

		list = new List<SettlementCell> ();

		GameObject pre = GameObject.Find ("SettlementPre");
		int x = 0;
		int y = 0;

		for(int s = 0 ; s < msg.items.Count ; s++ ){

			GameObject go = (GameObject)Instantiate (pre, transform);
			go.GetComponent<SettlementCell> ().PlayerId = msg.items [s].playerId;

			for( int t = 0 ; t < playerList.Count ; t++ ){
				if( go.GetComponent<SettlementCell> ().PlayerId == playerList[t].Id ){
					go.GetComponent<SettlementCell> ().head = playerList [t].Head.sprite;
				}
			}
			go.GetComponent<SettlementCell> ().Mark = msg.items [s].cedit.ToString();
			go.GetComponent<SettlementCell> ().Name = msg.items [s].nickName.ToString();
			go.GetComponent<SettlementCell> ().list = msg.items [s].cards;
			go.GetComponent<SettlementCell> ().CardType = msg.items [s].cardsType.ToString();
			go.GetComponent<SettlementCell> ().happy= msg.items [s].luck.ToString();

			x = s % 2;
			y = s/2;
			go.transform.parent = transform;

			if (x == 0) {
				x = -544;
			} else {
				x = 544;
			}

			go.transform.localPosition = new Vector3 ( x, 208 - y * 280, 0);
			list.Add ( go.GetComponent<SettlementCell> ());
		}

	}

	void OnDestroy(){
		
	}
		
	
	// Update is called once per frame
	void Update () {
	
	}
}
